package net.tools.web;

import java.io.*;
import java.security.*;

class TunnelCommand extends TunnelSerializable
{

	static final byte INITKEY = 0;

	static final byte CONNECT = 1;

	static final byte SEND = 2;

	static final byte RECEIVE = 3;

	static final byte CLOSE = 4;

	private String host;

	private int port;

	String getHost()
	{
		return host;
	}

	void setHost(String host)
	{
		this.host = host;
	}

	int getPort()
	{
		return port;
	}

	void setPort(int port)
	{
		this.port = port;
	}

	void writeBodyTo(DataOutputStream out) throws IOException
	{
		out.writeLong(getSeq());
		out.writeByte(getCmd());
		if(getCmd() == CONNECT)
		{
			out.writeUTF(host);
			out.writeInt(port);
		}
		if(getCmd() == SEND || getCmd() == INITKEY)
		{
			out.writeInt(getLen());
			out.write(getData(), 0, getLen());
		}
	}

	void readBodyFrom(DataInputStream in) throws IOException
	{
		setSeq(in.readLong());
		setCmd(in.readByte());
		if(getCmd() == CONNECT)
		{
			setHost(in.readUTF());
			setPort(in.readInt());
		}
		if(getCmd() == SEND || getCmd() == INITKEY)
		{
			setLen(in.readInt());
			in.readFully(getData(), 0, getLen());
		}
	}

}
